#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import stat
from enum import Enum
from common.env_common import get_install_head_path

PERMISSION_755 = stat.S_IRWXU | stat.S_IXGRP | stat.S_IRGRP | stat.S_IROTH | stat.S_IXOTH
PERMISSION_700 = stat.S_IRWXU
PERMISSION_555 = stat.S_IRUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH
PERMISSION_600 = stat.S_IRUSR | stat.S_IWUSR
PERMISSION_640 = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
PERMISSION_644 = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
PERMISSION_400 = stat.S_IRUSR
PERMISSION_444 = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
AUTH_METHOD_LIST = ["trust", "reject", "md5", "sha256", "cert", "gss"]


class DwsRetEnum(str, Enum):
    SUCCESS = "0"
    FAILED = "2"


class AgentTypeEnum(int, Enum):
    CLUSTER = 0
    PROXY = 1


class CopyGenerateType(int, Enum):
    GENERATE_BY_ROACH = 0
    GENERATE_BY_GDS = 1
    INVALID_GENERATE_TYPE = 2


class StorageRole(int, Enum):
    MASTER = 0
    SLAVE = 1


class IntrusiveMode(int, Enum):
    INTRUSIVE_MODE = 0
    NON_INTRUSIVE_MODE = 1
    INVALID_INTRUSIVE_MODE = 2


class RestoreProgressFileName(str, Enum):
    PREREQUISITE_PROGRESS = "prerequisite_progress"
    RESTORE_PROGRESS = "restore_progress"
    POST_JOB_PROGRESS = "post_job_progress"


class RestoreSubtaskName(str, Enum):
    WHILE_LIST_SUBTASK = "white_list_subtask"
    RESTORE_SUBTASK = "restore_subtask"
    CLEAN_SUBTASK = "clean_subtask"


class TaskType(int, Enum):
    BACKUP = 0,
    RESTORE = 1,
    DELETE = 2,


class BackupResType(str, Enum):
    CLUSTER = 'cluster'
    DATABASE = 'database'
    SCHEMA = 'schema'
    TABLE = 'table'


class DwsSubType(str, Enum):
    DWS_CLUSTER = "DWS-cluster"
    DWS_DATABASE = "DWS-database"
    DWS_SCHEMA = "DWS-schema"
    DWS_TABLE = "DWS-table"


class GDSTableStatus(int, Enum):
    RESTORE_INIT = 0
    RESTORE_FAILED = 1
    RESTORE_SUCCESS = 2


class RpcParamKey:
    APPLICATION = "application"
    TYPES = "types"
    FULL_COPY = "full"
    INCREMENT_COPY = "increment"
    COPY_ID = "copyId"
    INPUT_FILE_PREFFIX = "rpcInput_"
    OUTPUT_FILE_PREFFIX = "rpcOutput_"
    QUERY_PREVIOUS_CPOY = "QueryPreviousCopy"
    REPORT_JOB_DETAILS = "ReportJobDetails"
    # 查询脚本
    RPC_TOOL = f"{get_install_head_path()}/DataBackup/ProtectClient/Plugins/GeneralDBPlugin/bin/rpctool.sh"
    # 输入参数文件目录
    PARAM_FILE_PATH = f"{get_install_head_path()}/DataBackup/ProtectClient/Plugins/GeneralDBPlugin/tmp"
    # 输出结果文件目录
    RESULT_PATH = f"{get_install_head_path()}/DataBackup/ProtectClient/Plugins/GeneralDBPlugin/stmp"


class JsonKey(str, Enum):
    BACKUP_KEY = "dws_backup_id"
    HOST_NAME = "hostname"
    HOST_IP = "host_ip"
    CLUSTER_NODES = "cluster_nodes"
    STORAGE_ESN = "storage_esn"
    VERSION = "version"
    STORAGE_ID = "storage_id"
    AGENT_ID = "BackAgent_id"
    BACKUP_TOOL_TYPE = "backupToolType"
    METADATA_PATH = "metadataPath"
    COPY_ID = "copy_id"
    TOTAL_DATA_SIZE = "totalDataSize"
    STORAGE_TYPE = "storage_type"
    BACKUP_TIME = "backup_time"


class SupportProtocol(int, Enum):
    UNKNOWN = -1
    NFS = 1
    CIFS = 2
    NFS_CIFS = 3
    DATA_TURBO = 1024


class DwsBackupLabel(str, Enum):
    # 白名单子任务（{0}）执行成功。
    WHITE_SUB_JOB_SUC_LABEL = "dws_whitelist_subjob_success_label"
    # 数据保护代理主机（{0}）执行添加白名单子任务（{1}）失败。
    WHITE_SUB_JOB_FAILED_LABEL = "dws_whitelist_subjob_fail_label"
    # 数据保护代理主机（{0}）执行备份子任务（{1}）成功。
    BACKUP_SUBJOB_SUC_LABEL = "dws_backup_subjob_success_label"
    # 数据保护代理主机（{0}）执行备份子任务（{1}）失败。
    BACKUP_SUBJOB_FAILED_LABEL = "dws_backup_subjob_fail_label"
    # 待备份的表（[{0}]）不存在。
    BACKUP_TABLE_NOT_EXISTS_LABEL = "dws_backup_table_not_exist_label"
    # 数据保护代理主机（{0}）执行查询副本子任务（{1}）成功。
    QUERY_COPY_SUBJOB_SUC_LABEL = "dws_query_subjob_success_label"
    # 数据保护代理主机（{0}）执行查询副本子任务（{1}）失败。
    QUERY_COPY_SUBJOB_FAILED_LABEL = "dws_query_subjob_fail_label"
    # 数据保护代理主机（{0}）上GDS进程个数（{1}）小于最优值（{2}），GDS备份性能可能无法达到最优。
    GDS_COUNT_LESS_THAN_OPTIMAL_COUNT = "dws_backup_subjob_gds_performance_label"
    # 数据保护代理主机（{0}）正在执行备份子任务，待备份表的数量：（{1}），已备份表的数量：（{2}）。
    GDS_BACKUP_SUB_JOB_PROGESS = "dws_backup_subjob_gds_label"
    # 数据保护代理主机（{0}）正在执行备份子任务，已备份数据量：（{1}
    DWS_BACKUP_SUBJOB_ROACH_LABEL = "dws_backup_subjob_roach_label"
    # 待备份的Schema（[{0}]）不存在。"
    DWS_BACKUP_SUBJOB_ROACH_SCHEMA_LABEL = "dws_backup_subjob_roach_schema_label"
    # 存储单元（[{0}]）的文件系统（[{1}]）不可用。
    DWS_BACKUP_STORAGE_REPO_NOT_AVAILABLE_LABEL = "dws_backup_storage_repo_not_available_label"


class DwsDeployType(int, Enum):
    DEPLOY_IN_SERVER = 0,
    DEPLOY_IN_SANDBOX = 1


class DwsRoachPort(int, Enum):
    ROACH_PORT_START = 55000,
    ROACH_PORT_END = 55010


class DwsBackupStatus(str, Enum):
    UNVERIFIED = "UNVERIFIED"  # 表示备份成功，但未做过验证。验证是执行validate命令，只涉及DISK介质
    VALIDATED = "VALIDATED"  # 表示备份成功，并且成功通过验证。
    UNKNOWN = "UNKNOWN"  # 表示未获取到备份集状态。
    IN_PROGRESS = "IN PROGRESS"  # 表示备份正在进行。
    CORRUPTED = "CORRUPTED"  # 表示备份集验证未通过或删除过程中出现异常。
    DELETED = "DELETED"  # 表示备份集已删除。
    FAILED = "FAILED"  # 表示备份失败。
    NA = "NA"  # 表示无效备份，或备份成功但验证失败。判断为失败


class CommonRegexp:
    # backup_id 正则表达式
    backup_id_reg = r"^\d{4}\d{2}\d{2}_\d{2}\d{2}\d{2}$"


class RoachConstant:
    BUSINESS_CONFIG_FILE = "business_config.txt"
    DWS_FILESYSTEM_MOUNT_PATH = "/mnt/databackup"
    AGENT_STMP_PATH = f"{get_install_head_path()}/DataBackup/ProtectClient/ProtectClient-E/stmp"
    # 资源路径白名单
    WHITE_FILE_LIST = [DWS_FILESYSTEM_MOUNT_PATH, AGENT_STMP_PATH]
    # xbsa.conf文件默认路径
    XBSA_FILE_DEFAULT_PATH = "/home/backupagent/xbsa.conf"


class BusinessConfigType:
    # XBSA业务类型
    # 全备
    FULL_BACKUP_TYPE = 0
    # 增备
    INCREMENT_BACKUP_TYPE = 1
    # 全恢
    FULL_RESTORE_TYPE = 2
    # 差恢
    INCREMENT_RESTORE_TYPE = 3


class UserInfo:
    # root用户
    USER_ROOT = "root"
    # rdadmin用户
    USER_RDADMIN = "rdadmin"


class SubJobType(int, Enum):
    PRE_SUB_JOB = 0
    GENERATE_SUB_JOB = 1
    BUSINESS_SUB_JOB = 2
    POST_SUB_JOB = 3
